//
//  ShowCommitView.m
//  demoapp
//
//  Created by MingmingSun on 15/9/19.
//  Copyright (c) 2015年 pingplusplus. All rights reserved.
//

#import "ShowCommitView.h"
#import "BRPlaceholderTextView.h"

@interface ShowCommitView()

@property(nonatomic,strong) BRPlaceholderTextView *contentView;
@property(nonatomic,strong) UIButton *yyBut;

@end

@implementation ShowCommitView

- (instancetype)init{
    //solid frame,up validition is a just frame...
    CGRect solidRect = CGRectMake(0,0,280,200);
    if(!(self = [super initWithFrame:solidRect])){
        return nil;
    }
    self.aReplyID = 0;
    self.backgroundColor = [UIColor blackColor];
    
    self.contentView = [[BRPlaceholderTextView alloc] initWithFrame:CGRectMake(20, 20, 240, 125)];
    self.contentView.font = [UIFont systemFontOfSize:17.0f];
    self.contentView.placeholder = @"输入正文";
    [self addSubview:self.contentView];
    
    self.yyBut = [[UIButton alloc] initWithFrame:CGRectMake(20, 155, 240, 25)];
    [self.yyBut setBackgroundImage:[UIImage imageNamed:@"loginPress.png"] forState:UIControlStateNormal];
    [self.yyBut setTitle:@"回复" forState:UIControlStateNormal];
    [self.yyBut addTarget:self action:@selector(yyPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.yyBut];
    
    return self;
}

-(void)yyPressed:(id)sender{
    if(!self.contentView.text.length){
        [SVProgressHUD showErrorWithStatus:@"正文不可为空"];
        return;
    }
    if(self.contentView.text.length > 80){
        [SVProgressHUD showErrorWithStatus:@"正文不可超过80字"];
        return;
    }
    [self getShowReplys];
//    [self.myDelegate showPressed:nil];
}

- (void)getShowReplys {
    NSString *idStr = self.aReplyID?[NSString stringWithFormat:@"%i",self.aReplyID]:APPALL.showID;
    NSString *urlStr = self.aReplyID?@"show_rreply":@"show_reply";
    NSString *iidStr = self.aReplyID?@"replyId":@"id";
    NSDictionary *para =
    //    @{@"type":@"history",
    @{iidStr:idStr,
      @"title":@"",
      @"content":self.contentView.text,
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:urlStr withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah1....");
                    [self.myDelegate showPressed:nil];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

@end